﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package GameMode {
    import flash.events.*;
    import flash.display.*;
    import flash.geom.*;
    import Engine.Helpers.*;
    import Events.*;
    import Classes.*;
    import Transactions.*;
    import Classes.util.*;
    import Display.*;
    import Display.MarketUI.*;
    import Engine.*;

    public class GMExpand extends GMDefault {

        protected static var m_MarketCell:MarketCell = null;

        protected var m_size:Vector2;
        protected var m_area:Sprite;
        protected var m_itemName:String;
        protected var m_expansionRect:Rectangle;
        protected var m_locationValid:Boolean;
        protected var m_rect:Rectangle;

        public function GMExpand(_arg1:String){
            this.m_size = new Vector2();
            this.m_rect = new Rectangle();
            super();
            this.m_itemName = _arg1;
            var _local2:Item = Global.gameSettings.getItemByName(_arg1);
            if (_local2){
                this.m_size.x = _local2.expansionWidth;
                this.m_size.y = _local2.expansionHeight;
            };
        }
        public static function resetMarketCell():void{
            m_MarketCell = null;
        }
        public static function setMarketCell(_arg1:MarketCell):void{
            m_MarketCell = _arg1;
        }

        override public function enableMode():void{
            super.enableMode();
            var _local1:Vector2 = IsoMath.screenPosToTilePos(GlobalEngine.viewport.mouseX, GlobalEngine.viewport.mouseY);
            this.m_expansionRect = Global.world.m_territory.getClosestAdjacentRect(_local1);
            this.m_locationValid = true;
            this.drawExpansionArea(0xFF);
        }
        override public function disableMode():void{
            super.disableMode();
            GlobalEngine.viewport.overlayBase.removeChild(this.m_area);
        }
        override public function onMouseMove(_arg1:MouseEvent):Boolean{
            super.onMouseMove(_arg1);
            var _local2:Number = ((_arg1)!=null) ? _arg1.stageX : Global.stage.mouseX;
            var _local3:Number = ((_arg1)!=null) ? _arg1.stageY : Global.stage.mouseY;
            var _local4:Vector2 = IsoMath.screenPosToTilePos(_local2, _local3);
            this.m_expansionRect = Global.world.m_territory.getClosestAdjacentRect(_local4);
            this.m_locationValid = this.checkLocationValid();
            var _local5:uint = (this.m_locationValid) ? 0xFF : 0xFF0000;
            this.drawExpansionArea(_local5);
            return (true);
        }
        protected function checkLocationValid():Boolean{
            var _local1:Boolean;
            _local1 = !(Global.world.rectIntersectsTerritory(this.m_expansionRect));
            var _local2:Boolean = Global.world.citySim.waterManager.positionValidForExpansion(this.m_expansionRect);
            if (!_local2){
                _local1 = false;
            };
            return (_local1);
        }
        override protected function handleClick(_arg1:MouseEvent):void{
            if (this.m_locationValid){
                UI.displayMessage(ZLoc.t("Dialogs", "PlaceExpansion"), GenericPopup.TYPE_YESNO, this.onConfirmationDialogClose);
            };
        }
        protected function onConfirmationDialogClose(_arg1:GenericPopupEvent):void{
            var _local2:Array;
            var _local3:Object;
            var _local4:int;
            var _local5:int;
            var _local6:Wilderness;
            var _local7:int;
            if (_arg1.button == GenericPopup.YES){
                Global.player.gold = (Global.player.gold - Global.gameSettings.getItemByName(this.m_itemName).cost);
                Global.world.expandMap(this.m_expansionRect);
                Global.world.addGameMode(new GMPlay());
                _local2 = Global.world.wildernessSim.getTreesInRect(this.m_expansionRect);
                for each (_local3 in _local2) {
                    _local6 = new Wilderness(_local3.itemName);
                    _local7 = TWorldState.getNextTempID();
                    _local6.setId(_local7);
                    _local3.id = _local7;
                    _local6.setState("static");
                    _local6.setOuter(Global.world);
                    _local6.setDirection(_local3.dir);
                    _local6.setPosition(_local3.x, _local3.y);
                    _local6.isMoveLocked = true;
                    _local6.attach();
                    _local3.wildernessObj = _local6;
                };
                Global.world.createOverlayBackground();
                GameTransactionManager.addTransaction(new TExpandFarm(this.m_itemName, new Vector2(this.m_expansionRect.x, this.m_expansionRect.y), _local2));
                _local4 = ExpansionManager.instance.getNextExpansionPermitRequirement();
                if (Global.player.m_licenses[this.m_itemName] == 1){
                    Global.player.deleteLicense(this.m_itemName);
                };
                _local5 = Math.min(Global.player.inventory.getItemCountByName(Item.PERMIT_ITEM), _local4);
                Global.player.inventory.removeItems(Item.PERMIT_ITEM, _local5);
                Global.player.expansionsPurchased++;
                Global.player.expansionCostLevel++;
                Global.player.populationObservedOnLastExpansion = Global.world.citySim.getPopulation();
                if (m_MarketCell != null){
                    m_MarketCell.performUpdate(true);
                };
                resetMarketCell();
                Global.world.citySim.roadManager.onGameLoaded(null);
            };
        }
        protected function drawExpansionArea(_arg1:uint):void{
            var _local2:Boolean;
            if (!this.m_area){
                this.m_area = new Sprite();
                _local2 = true;
            };
            var _local3:Vector2 = IsoMath.tilePosToPixelPos(this.m_expansionRect.x, this.m_expansionRect.y);
            var _local4:Vector2 = IsoMath.tilePosToPixelPos(this.m_expansionRect.x, this.m_expansionRect.bottom);
            var _local5:Vector2 = IsoMath.tilePosToPixelPos(this.m_expansionRect.right, this.m_expansionRect.bottom);
            var _local6:Vector2 = IsoMath.tilePosToPixelPos(this.m_expansionRect.right, this.m_expansionRect.y);
            var _local7:Number = 2;
            this.m_area.graphics.clear();
            this.m_area.graphics.lineStyle(_local7, _arg1);
            this.m_area.graphics.moveTo(_local5.x, _local5.y);
            this.m_area.graphics.lineTo(_local6.x, _local6.y);
            this.m_area.graphics.lineTo(_local3.x, _local3.y);
            this.m_area.graphics.lineTo(_local4.x, _local4.y);
            this.m_area.graphics.lineTo(_local5.x, _local5.y);
            if (_local2){
                GlobalEngine.viewport.overlayBase.addChild(this.m_area);
            };
        }
        override protected function isObjectHighlightable(_arg1:GameObject):Boolean{
            return (false);
        }
        override protected function isObjectSelectable(_arg1:GameObject):Boolean{
            return (false);
        }
        override protected function isObjectDraggable(_arg1:GameObject):Boolean{
            return (false);
        }

    }
}//package GameMode 
